from flask_cors import CORS

# 新增api_server.py
from flask import Flask, jsonify
from mysql_utils import db_manager
from flask_socketio import SocketIO, emit

# 原有数据库模块保留
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*")  # 允许跨域

# WebSocket连接事件处理
@socketio.on('connect')
def handle_connect():
    print('WebSocket client connected')

# 跨域支持
CORS(app)

@app.route('/api/interactions', methods=['GET'])
def get_all_interactions():
    records = db_manager.get_all_records()
    return jsonify({'data': records, 'count': len(records)})

@app.route('/api/recent', methods=['GET'])
def get_recent_interactions():
    records = db_manager.get_recent_10()
    return jsonify({'data': records, 'count': len(records)})

if __name__ == '__main__':
    app.run(host='10.149.158.72', port=8090, threaded=True)
    
    # # 启动WebSocket服务
    # socketio.run(app, host='0.0.0.0', port=5000, debug=True)